package com.javasm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.AftersaleB2cRetreatCriteria;
import com.javasm.domin.entity.AftersaleB2cRetreat;
import com.javasm.domin.vo.AftersaleB2cRetreatVo;
import com.javasm.mapper.AftersaleB2cRetreatMapper;
import com.javasm.service.AftersaleB2cRetreatService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.AftersaleB2cRetreatTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class AftersaleB2cRetreatServiceImpl extends BaseServiceimpl<AftersaleB2cRetreat> implements AftersaleB2cRetreatService {

    private final AftersaleB2cRetreatMapper aftersaleB2cRetreatMapper;
    private final AftersaleB2cRetreatTransfer aftersaleB2cRetreatTransfer;

    @Override
    public PageResult<AftersaleB2cRetreatVo> searchPage(AftersaleB2cRetreatCriteria aftersaleB2cRetreatCriteria) {
        //开启分页
        PageHelper.startPage(aftersaleB2cRetreatCriteria.getCurrentPage(),aftersaleB2cRetreatCriteria.getPageSize());
        //条件创建
        LambdaQueryWrapper<AftersaleB2cRetreat> lambda = new QueryWrapper<AftersaleB2cRetreat>().lambda();
        if(aftersaleB2cRetreatCriteria.getEstablishId()!=null&&aftersaleB2cRetreatCriteria.getEstablishId()!=""){
            lambda.like(AftersaleB2cRetreat::getEstablishId, aftersaleB2cRetreatCriteria.getEstablishId());
        }
        if(!StringUtils.isEmpty(aftersaleB2cRetreatCriteria.getEstablishstartTime())){
            lambda.between(AftersaleB2cRetreat::getEstablishTime, aftersaleB2cRetreatCriteria.getEstablishstartTime(), aftersaleB2cRetreatCriteria.getEstablishendTime());
        }

        if(aftersaleB2cRetreatCriteria.getOrderId()!=null&&aftersaleB2cRetreatCriteria.getOrderId()!=0){
            lambda.eq(AftersaleB2cRetreat::getOrderId, aftersaleB2cRetreatCriteria.getOrderId());
        }
        if(aftersaleB2cRetreatCriteria.getApproverId()!=null&&aftersaleB2cRetreatCriteria.getApproverId()!=""){
            lambda.like(AftersaleB2cRetreat::getApproverId, aftersaleB2cRetreatCriteria.getApproverId());
        }
        if(aftersaleB2cRetreatCriteria.getNumberState()!=null&&aftersaleB2cRetreatCriteria.getNumberState()!=0){
            lambda.eq(AftersaleB2cRetreat::getNumberState, aftersaleB2cRetreatCriteria.getNumberState());
        }
        if(!StringUtils.isEmpty(aftersaleB2cRetreatCriteria.getStartTime())){
            lambda.between(AftersaleB2cRetreat::getCreateTime, aftersaleB2cRetreatCriteria.getStartTime(), aftersaleB2cRetreatCriteria.getEndTime());
        }

        List<AftersaleB2cRetreat> aftersaleOuts = aftersaleB2cRetreatMapper.selectList(lambda);
        /*获取总条数*/
        PageInfo<AftersaleB2cRetreat> aftersaleOutPageInfo = new PageInfo<>(aftersaleOuts);
        return new PageResult<AftersaleB2cRetreatVo>(aftersaleOutPageInfo.getTotal(),aftersaleB2cRetreatTransfer.jotoVo(aftersaleOuts));


    }
}
